<%@ page import="brundfahrt.Invited" %>
<!doctype html>
<html>
	<head>
		<meta name="layout" content="main">
		<title><g:message code="invited.select.persons" default="Select Persons" /></title>
		<r:require modules="jquery"/>
	</head>
	<body>
		<a href="#selectpersons-invited" class="skip" tabindex="-1"><g:message code="default.link.skip.label" default="Skip to content&hellip;"/></a>
		<div id="selectpersons-invited" class="content scaffold-create" role="main">
			<h1><g:message code="invited.select.persons" default="Select Persons"/></h1>
			<g:if test="${message}">
				<div class="message" role="status">${message}</div>
			</g:if>					
			<g:hasErrors bean="${filterResultInstance}">
			<ul class="errors" role="alert">
				<g:eachError bean="${filterResultInstance}" var="error">
				<li <g:if test="${error in org.springframework.validation.FieldError}">data-field-id="${error.field}"</g:if>><g:message error="${error}"/></li>
				</g:eachError>
			</ul>
			</g:hasErrors>
			<g:form>
               	<fieldset class="form">
                    <table>
                    	<thead>
		                 	 <th><g:message code="person.selection.column.label" default="Select"/></th>
		                 	 <th><g:message code="unit.name.label" default="Unit Name" />
							 <th><g:message code="person.label" default="Person" />
							 <th><g:message code="specific.address.label" default="Specific Address" />
							 <th><g:message code="private.address.label" default="Private Address" />
							 <%--<th><g:message code="address.label" default="Address" />
                    	--%></thead>
                        <tbody>   
				             <g:each in="${filterResultInstance.returnedPersons}" status="i" var="returnedPersonsInstance">
				                 <tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
				                 	
				                 	
				                 	<td>
					                 	 <g:hiddenField name="returnedPersons[${i}].commonService.id" value="${returnedPersonsInstance.commonService.id}"/>
					                 	 <g:checkBox name="returnedPersons[${i}].selected" value="${returnedPersonsInstance.selected}"/>
				                 	</td>
				                 	<g:set var="specificAddress" value=""/>
				                 	<g:if test="${returnedPersonsInstance.commonService.participant.class == brundfahrt.Company}">
				                 		<g:set var="specificAddress" value="${returnedPersonsInstance.commonService.participant.getPersonWorkAddress(returnedPersonsInstance?.person)}"/>
				                 	</g:if>
				                 	<g:elseif test="${returnedPersonsInstance.commonService.participant.class == brundfahrt.Group}">
				                 		<g:set var="specificAddress" value="${returnedPersonsInstance.commonService.participant.getAddress()}"/>
				                 	</g:elseif>

				                 	<g:set var="multipleAddresses" value="${specificAddress && returnedPersonsInstance.person.getAddress() &&
				                 											specificAddress != returnedPersonsInstance.person.getAddress() }"/>
				                 	<td>
				                 		<g:if test="${returnedPersonsInstance.commonService.participant.class != brundfahrt.Person}">
				                 			${returnedPersonsInstance.commonService.participant}				                 			
				                 		</g:if>
				                 	</td>
   			                     	<td>${returnedPersonsInstance.person}</td>
   			                     	<td>
   			                     		<g:if test="${returnedPersonsInstance.commonService.participant.class != brundfahrt.Person}">
				                 			<g:if test="${multipleAddresses}">
				                 				<g:radio name="returnedPersons[${i}].addressSelection" value="S" checked="true"/>
				                 			</g:if>   			                     		   			                     		
   			                     			${specificAddress}
 			                     		</g:if>
   			                     	</td>
   			                     	<td>
			                 			<g:if test="${multipleAddresses}">
			                 				<g:radio name="returnedPersons[${i}].addressSelection" value="P"/>
			                 			</g:if>   			                     		   			                     		   			                     	
   			                     		${returnedPersonsInstance.person.getAddress()}
   			                     	</td>
				                 </tr>
             				 </g:each>
                                       
                        </tbody>
                    </table>
               	
               	
				</fieldset>
				<r:script>
					function setAllCheckboxes(status){
						$("INPUT[type='checkbox']").attr('checked', status);
					}				
				</r:script>
				
                <div class="actions">
                    <span class="button"><g:submitButton name="selectAll" onclick="setAllCheckboxes(true);return false;" class="select" value="${g.message(code: 'default.button.select.all.label', default: 'Select All')}" /></span>
                    <span class="button"><g:submitButton name="deselectAll" onclick="setAllCheckboxes(false);return false;" class="deselect" value="${g.message(code: 'default.button.deselect.all.label', default: 'Deselect All')}" /></span>
                </div>
                				
                <div class="buttons">
                    <span class="button"><g:submitButton name="addSelected" class="next" value="${g.message(code: 'invited.button.add.invited.label', default: 'Add Selected')}" /></span>
                    <span class="button"><g:submitButton name="backToFilter" class="back" value="${g.message(code: 'invited.button.back.to.filter.label', default: 'Back To Filter')}" /></span>
					<span class="button"><g:submitButton name="cancel" class="cancel" value="${g.message(code: 'default.button.cancel.label', default: 'Cancel')}" /></span>                    
                </div>
            </g:form>    
		</div>
	</body>
</html>
